package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ocom.common.entity.access.EntityMjAreaInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoAreaInfo;
import com.ocom.srvpaccess.service.AreaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AreaInfoServiceImpl extends ServiceImpl<IDaoAreaInfo, EntityMjAreaInfo> implements AreaInfoService {

    //注入持久层
    @Autowired
    private IDaoAreaInfo daoAreaInfo;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjAreaInfo getAreaInfo(Long companyId, Long areaId) {
        QueryWrapper<EntityMjAreaInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("area_id",areaId);
        query.last("limit 1");
        return daoAreaInfo.selectOne(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjAreaInfo> selectListAreaId(Long comId,Integer regionId) {
        QueryWrapper<EntityMjAreaInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("region_id",regionId);
        return daoAreaInfo.selectList(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public int selectRegionId(Long comId, int areaId) {
        QueryWrapper<EntityMjAreaInfo> wrapper = new QueryWrapper<>();
        wrapper.select("region_id");
        wrapper.eq("area_id",areaId);
        EntityMjAreaInfo entityMjAreaInfo=daoAreaInfo.selectOne(wrapper);
        if (entityMjAreaInfo!=null){
            if (entityMjAreaInfo.getRegionId()!=null){
                return Math.toIntExact(entityMjAreaInfo.getRegionId());
            }
        }
        return 0;
    }
}
